
#include <material/materialeditor/textureslot.h>
#include <session.h>
#include <QFileInfo>
#include <QMouseEvent>

cceTextureSlot::cceTextureSlot(QWidget *parent)
	: QLabel(parent)
	, _texture(false)
	, _clicked (false)
{
	setAcceptDrops(true);
	setMouseTracking(true);
	setMinimumSize(96, 96);
	setMaximumSize(96, 96);
	setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

	setAlignment(Qt::AlignCenter);

	setFrameShape(QFrame::Panel);
	setFrameShadow(QFrame::Plain);

}


cceTextureSlot::~cceTextureSlot()
{

}

void cceTextureSlot::SetTexture(cceMaterialTexture *texture)
{
	_texture = texture;

	QString filename = "/" CCE_MATERIAL_TEXTURES_FOLDER "/"+_texture->GetFilename();
	filename = cceSession::Get()->GetFileName(filename);
	QImage img (filename);
	if (img.load(filename))
		{
			img = img.scaled(96, 96,  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
			QPixmap pm = QPixmap::fromImage(img);
			setPixmap(pm);
		}
	else
		{
			setPixmap(QPixmap());
		}
}



cceMaterialTexture* cceTextureSlot::GetTexture() const
{
	return _texture;
}

void cceTextureSlot::enterEvent(QEvent *event)
{
	Q_UNUSED(event);
	if (_texture)
		{
			setCursor(QCursor(Qt::PointingHandCursor));
		}
	else
		{
			setCursor(QCursor(Qt::ArrowCursor));
		}
}


void cceTextureSlot::leaveEvent(QEvent *event)
{
	Q_UNUSED(event);
	setCursor(QCursor(Qt::ArrowCursor));
}



void cceTextureSlot::dragEnterEvent(QDragEnterEvent *event)
{

	const QMimeData* mimeData = event->mimeData();

	if (mimeData->hasFormat (CCE_FILESYSTEM_MIME))
		{
			QString name = mimeData->data(CCE_FILESYSTEM_MIME);
			QFileInfo info (name);
			QString ext = info.suffix().toLower().trimmed();
			if (ext == "jpg" || ext == "jpeg" ||
					ext == "png" || ext == "tga" ||
					ext == "bmp")
				{
					event->accept();
					return;
				}
		}
	else if (mimeData->hasFormat(CCE_MATERIAL_TEXTURENAME_MIME))
		{
			QString name = mimeData->data(CCE_MATERIAL_TEXTURENAME_MIME);
			event->accept();
			return;
		}

	event->ignore();
}

void cceTextureSlot::dropEvent(QDropEvent *event)
{
	const QMimeData* mimeData = event->mimeData();

	if (mimeData->hasFormat (CCE_FILESYSTEM_MIME))
		{
			QString name = mimeData->data(CCE_FILESYSTEM_MIME);
			QFileInfo info (name);
			QString ext = info.suffix().toLower().trimmed();
			if (ext == "jpg" || ext == "jpeg" ||
					ext == "png" || ext == "tga" ||
					ext == "bmp")
				{
					cceMaterialTexture* texture = cceSession::Get()->GetMaterialManager()->GetTextureByFilename(name, true);
					SetTexture (texture);
					return;
				}
		}
	else if (mimeData->hasFormat(CCE_MATERIAL_TEXTURENAME_MIME))
		{
			QString name = mimeData->data(CCE_MATERIAL_TEXTURENAME_MIME);
			cceMaterialTexture* texture = cceSession::Get()->GetMaterialManager()->GetTexture(name);
			SetTexture (texture);
			return;
		}

	event->ignore();
}

void cceTextureSlot::mousePressEvent(QMouseEvent *event)
{
	if (!_texture)
		{
			return;
		}

	grabMouse();
	_clicked = true;
}

void cceTextureSlot::mouseReleaseEvent(QMouseEvent *event)
{
	if (!_texture|| !_clicked)
		{
			return;
		}
	releaseMouse();
	if (!rect().contains(event->pos()))
		{
			return;
		}

	emit ShowTexture (_texture);
}
